package com.bong.ring.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.bong.ring.R;

import java.util.ArrayList;

/**
 * Created by panpan on 2018/2/1.
 */

public class DeviceListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<String[]> data = new ArrayList<>();
    private View.OnClickListener listener = null;

    public DeviceListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public String[] getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_device, null, false);
            holder = new ViewHolder();
            holder.name1 = convertView.findViewById(R.id.name1);
            holder.name = convertView.findViewById(R.id.name);
            holder.addr = convertView.findViewById(R.id.addr);
            holder.bond = convertView.findViewById(R.id.button_bond);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String[] info = data.get(position);
        holder.name1.setText("b");
        holder.name.setText(info[0]);
        holder.addr.setText("MAC  " + info[1]);
        holder.bond.setId(position);
        if (listener != null)
            holder.bond.setOnClickListener(listener);
        return convertView;
    }

    public void add(String[] data) {
        this.data.add(data);
    }

    public class ViewHolder {
        TextView name1, name, addr;
        Button bond;
    }

    public void setListener(View.OnClickListener listener) {
        this.listener = listener;
    }
}
